#include <iostream>
#include "EnemyManager.h"

EnemyManager::EnemyManager() {
}
EnemyManager::~EnemyManager() {
}

void EnemyManager::Initialize(sf::RenderWindow* win) {
	m_renderWindow = win;
	octopiCount = 0;
	side = true;
	alreadyOnLeft = false;
	alreadyOnRight = false;

	friendly = false;
	m_spawnClock = new sf::Clock();
	m_spawnDelayClock = new sf::Clock();

	m_octTexture = new sf::Texture();
	m_octTexture->loadFromFile("Assets/Images/Enemies/Enemy_Shadow_Sprite.png");
	m_octTextureAtc = new sf::Texture();
	m_octTextureAtc->loadFromFile("Assets/Images/Enemies/Enemy_Octopus_Attack.png");

	m_shkTexture = new sf::Texture();
	m_shkTexture->loadFromFile("Assets/Images/Enemies/Enemy_Shadow_Sprite.png");
	m_shkTextureAtc = new sf::Texture();
	m_shkTextureAtc->loadFromFile("Assets/Images/Enemies/Enemy_Shark_Attack.png");
	m_shkTextureAtc2 = new sf::Texture();
	m_shkTextureAtc2->loadFromFile("Assets/Images/Enemies/Enemy_Shark_Attack2.png");
	m_shkTextureDead = new sf::Texture();
	m_shkTextureDead->loadFromFile("Assets/Images/Enemies/Enemy_Shark_Dead.png");

	m_dlpTexture = new sf::Texture();
	m_dlpTexture->loadFromFile("Assets/Images/Enemies/Enemy_Shadow_Sprite.png");
	m_dlpTextureAtc = new sf::Texture();
	m_dlpTextureAtc->loadFromFile("Assets/Images/Enemies/Enemy_Dolphin_Attack.png");

	m_splashTexture = new sf::Texture();
	m_splashTexture->loadFromFile("Assets/Images/Enemies/Enemy_Splash.png");

	m_takeDamageTexture = new sf::Texture();
	m_takeDamageTexture->loadFromFile("Assets/Images/Damage_ Feedback.png");

	m_bloodTexture = new sf::Texture();
	m_bloodTexture->loadFromFile("Assets/Images/Enemy_Death2.png");

	timeBetweenSpawns = 5.0f;
	spawnRate = 1.5f;

	numberOfEnemies = 6;

	totalEnemiesSpawned = 0;

	wave = 1;
}

void EnemyManager::Update(Player*	player, TextManager* textManager) {
	spawnRate = 3 - (wave * 0.2f);
	if(spawnRate < 0.5f){
		spawnRate = 0.5f;
	}
	timeBetweenSpawns = 10 - (wave * 0.5f);
	if(timeBetweenSpawns < 0.5f){
		timeBetweenSpawns = 0.5f;
	}
//	std::cout << spawnRate << std::endl << timeBetweenSpawns << std::endl;
	//std::cout << wave << std::endl;
	srand((unsigned)time(0)); 
	random_integer = (rand()%2)+1;
	int random_integer2 = (rand()%3)+1;

	for(int i = 0; i < m_arrayOfOctopi.size(); i++) {
		if( m_arrayOfOctopi[i]->GetSide() == 0 && m_arrayOfOctopi[i]->m_isFirst == false) {
			m_arrayOfOctopi[i]->m_isFirst = true;
			break;
		} else if ( m_arrayOfOctopi[i]->GetSide() == 1 &&  m_arrayOfOctopi[i]->m_isFirst == false) {
			m_arrayOfOctopi[i]->m_isFirst = true;
			break;
		}
	}


#pragma region OldCode
	/*
	if (octopiCount < 2 && m_spawnClock->getElapsedTime().asSeconds() > 2.0f) {
		m_spawnClock->restart();
		octopiCount++;
		Octopi* octopiObj = new Octopi;

		if (alreadyOnLeft == false && alreadyOnRight == false) {

			if (random_integer == 1) {
				//std::cout << "SFGASDFA" << std::endl;
				octopiObj->SetPosX(400.0f);
				octopiObj->SetSide(0);
				alreadyOnLeft = true;
			} else if (random_integer == 2) {
			//	std::cout << "SFGASDFA" << std::endl;
				octopiObj->SetPosX(1300.0f);
				octopiObj->SetSide(1);
				alreadyOnRight = true;
			}
		} else if (alreadyOnLeft == true && alreadyOnRight == false) {
			octopiObj->SetPosX(1300.0f);
			octopiObj->SetSide(1);
			alreadyOnRight = true;
		} else if (alreadyOnLeft == false && alreadyOnRight == true) {
			octopiObj->SetPosX(400.0f);
			octopiObj->SetSide(0);
			alreadyOnLeft = true;
		}


		if(random_integer2 == 1){
			octopiObj->Initialize(m_renderWindow, m_octTexture, m_octTextureAtc, m_splashTexture);		
		}else if(random_integer2 == 2){
			octopiObj->Initialize(m_renderWindow, m_shkTexture, m_shkTextureAtc, m_splashTexture);	
		}


		m_arrayOfOctopi.push_back(octopiObj);
	}
	*/

#pragma endregion


	if (m_spawnDelayClock->getElapsedTime().asSeconds() > timeBetweenSpawns) {
		if(totalEnemiesSpawned == numberOfEnemies) {
			wave++;
			m_spawnDelayClock->restart();
			totalEnemiesSpawned = 0;
		}
	if (octopiCount < numberOfEnemies && m_spawnClock->getElapsedTime().asSeconds() > spawnRate) {
		m_spawnClock->restart();
		octopiCount++;
		totalEnemiesSpawned++;
		Octopi* octopiObj = new Octopi;

		//if (alreadyOnLeft == false && alreadyOnRight == false) {

			if (random_integer == 1) {
				//std::cout << "SFGASDFA" << std::endl;
				octopiObj->SetPosX(580.0f);
				octopiObj->SetSide(0);
				//alreadyOnLeft = true;
			} else if (random_integer == 2) {
			//	std::cout << "SFGASDFA" << std::endl;
				octopiObj->SetPosX(1150.0f);
				octopiObj->SetSide(1);
				//alreadyOnRight = true;
			}
		if(random_integer2 == 1){
			octopiObj->Initialize(m_renderWindow, m_octTexture, m_octTextureAtc, m_splashTexture, m_shkTextureDead, m_takeDamageTexture, false, 1, m_bloodTexture);		
		}else if(random_integer2 == 2){
			if(random_integer == 1){
				octopiObj->Initialize(m_renderWindow, m_shkTexture, m_shkTextureAtc2, m_splashTexture, m_shkTextureDead, m_takeDamageTexture, false, 2, m_bloodTexture);	
			}else if(random_integer == 2){
				octopiObj->Initialize(m_renderWindow, m_shkTexture, m_shkTextureAtc, m_splashTexture, m_shkTextureDead, m_takeDamageTexture, false, 2, m_bloodTexture);			
			}
		}else if(random_integer2 == 3){
			octopiObj->Initialize(m_renderWindow, m_dlpTexture, m_dlpTextureAtc, m_splashTexture, m_shkTextureDead, m_takeDamageTexture, true, 3, m_bloodTexture);
		}

		m_arrayOfOctopi.push_back(octopiObj);
	}
	}





	/*
	if (m_arrayOfOctopi.size() == 0) {
		octopiCount = 0;
	}
	if (m_arrayOfOctopi.size() == 1) {
		octopiCount = 1;
	}
	*/
	octopiCount = m_arrayOfOctopi.size();

	
	for (int i = 0; i <m_arrayOfOctopi.size(); i++) {
		if(m_arrayOfOctopi[i]->m_isDead == true) {
			if(m_arrayOfOctopi[i]->GetSide() == 0) {
				//alreadyOnLeft = false;
			} else if (m_arrayOfOctopi[i]->GetSide() == 1) {
				//alreadyOnRight = false;
			}
			m_arrayOfOctopi[i]->Cleanup();
			delete m_arrayOfOctopi[i];
			m_arrayOfOctopi.erase(m_arrayOfOctopi.begin() + i);
		}
	}
	for(int i = m_arrayOfOctopi.size()-1; i >= 0; i--) {	
			m_arrayOfOctopi[i]->Update(player, textManager);	
	}
}

void EnemyManager::Cleanup() {
	for (int i = 0; i < m_arrayOfOctopi.size(); i++) {
		m_arrayOfOctopi[i]->Cleanup();
		delete m_arrayOfOctopi[i];
		m_arrayOfOctopi.erase(m_arrayOfOctopi.begin() + i);
	}
	delete m_spawnClock;
	delete m_octTexture;
	delete m_octTextureAtc;
	delete m_shkTexture;
	delete m_shkTextureAtc;
	delete m_shkTextureDead;
	delete m_dlpTexture;
	delete m_dlpTextureAtc;
	delete m_splashTexture;

}